
# REQUIRES: Data/Roper Iraq War Opinion Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Roper/Cleaning.R")

################# Import Data #################

iraq_withdraw <- c("Data/Roper Data for Analysis.csv")
iraq_withdraw <- lapply(iraq_withdraw, read.csv) %>% bind_rows()
iraq_withdraw <- iraq_withdraw[(iraq_withdraw$Campaign == "Iraq"),]

iraq_support <- c("Data/Roper Iraq War Opinion Data.csv")
iraq_support <- lapply(iraq_support, read.csv) %>% bind_rows()

################# Cleaning Support Data #################

iraq_support <- iraq_support[(iraq_support$Support == "Not Support"),]

temp <- aggregate(iraq_support$RespPct, by=list(Category=iraq_support$QuestionID), FUN=sum)
temp <- temp %>% rename(
     QuestionID = Category,
     NoSupport = x
  )

temp2 <- iraq_support
temp2$tally <- 1
temp2 <- temp2%>% group_by(QuestionID) %>% mutate(ticker = cumsum(tally))
temp2 <- temp2[(temp2$ticker == 1),]
temp2 <- temp2[ , !(names(temp2) %in% c("RespPct", "tally", "ticker"))]

iraq_support <- left_join(temp, temp2, by = "QuestionID")

rm(temp, temp2)

################# Join Withdraw and Support #################

# Withdraw
short_withdraw <- iraq_withdraw[ , (names(iraq_withdraw) %in% c("Pollingorg", "Startdate", "Withdraw"))]

short_withdraw$Pollingorg_char <- as.character(short_withdraw$Pollingorg)
short_withdraw$Startdate_char <- as.character(short_withdraw$Startdate)
short_withdraw$Start_polling <- paste(short_withdraw$Startdate_char, short_withdraw$Pollingorg_char)
short_withdraw$tally <- 1
short_withdraw <- short_withdraw%>% group_by(Start_polling) %>% mutate(ticker = cumsum(tally))

## Take the average Withdraw of each Withdraw poll (multiple questions per some polls)
temp_withdraw <- aggregate(short_withdraw$Withdraw, by=list(Category=short_withdraw$Start_polling), FUN=mean)

temp_withdraw <- temp_withdraw %>% rename(
  Start_polling = Category,
  Withdraw = x
)

# Support
short_support <- iraq_support[ , (names(iraq_support) %in% c("Pollingorg", "Startdate", "NoSupport"))]

short_support$Pollingorg_char <- as.character(short_support$Pollingorg)
short_support$Startdate_char <- as.character(short_support$Startdate)
short_support$Start_polling <- paste(short_support$Startdate_char, short_support$Pollingorg_char)
short_support$tally <- 1
short_support <- short_support%>% group_by(Start_polling) %>% mutate(ticker = cumsum(tally))

## Take the average NoSupport of each Support poll (multiple questions per some polls)
temp_support <- aggregate(short_support$NoSupport, by=list(Category=short_support$Start_polling), FUN=mean)

temp_support <- temp_support %>% rename(
  Start_polling = Category,
  NoSupport = x
)

## Some of the organizations did not have perfectly matching names so manual assignment was used
iraq_withdraw_support <- full_join(temp_withdraw, temp_support, by = "Start_polling")
iraq_withdraw_support <- iraq_withdraw_support %>%
  mutate(NoSupport = ifelse(Start_polling == "2003-12-14 CBS News; New York Times", 33.00000, iraq_withdraw_support$NoSupport))
iraq_withdraw_support <- iraq_withdraw_support %>%
    mutate(NoSupport = ifelse(Start_polling == "2004-09-12 CBS News; New York Times", 44.00000, iraq_withdraw_support$NoSupport))
iraq_withdraw_support <- iraq_withdraw_support %>%
  mutate(NoSupport = ifelse(Start_polling == "2005-10-03 CBS News; New York Times", 60.00000, iraq_withdraw_support$NoSupport))
iraq_withdraw_support <- iraq_withdraw_support %>%
  mutate(NoSupport = ifelse(Start_polling == "2006-03-10 Hart and Poll # 2006-6061: Congress/Health Care/Economy/Immigration/Middle East/Iraq Research Companies", 61.00000, iraq_withdraw_support$NoSupport))
iraq_withdraw_support <- iraq_withdraw_support %>%
  mutate(NoSupport = ifelse(Start_polling == "2010-08-11 GfK Roper Public Affairs & Corporate Communications", 54.50000, iraq_withdraw_support$NoSupport))

iraq_withdraw_support <- iraq_withdraw_support[!is.na(iraq_withdraw_support$Withdraw),]

rm(temp_withdraw, temp_support)

# Add the date column
temp <- short_withdraw %>%
  dplyr::select(Start_polling, Startdate)

iraq_withdraw_support <- left_join(iraq_withdraw_support, temp, by = "Start_polling")
rm(temp)

write.csv(iraq_withdraw_support,"Data/Iraq_Withdraw_Support.csv",row.names=FALSE)

################# Join Withdraw and Support #################

# Paired t-test of average support vs. average withdrawal support
t.test(iraq_withdraw_support$Withdraw, iraq_withdraw_support$NoSupport, paired = TRUE, alternative = "two.sided", na.action = "na.omit")

# Line Graph for Withdraw and Support

temp_s <- iraq_withdraw_support[ , (names(iraq_withdraw_support) %in% c("Start_polling", "Startdate", "Withdraw"))]
temp_s <- temp_s %>% rename(SurveyMean = Withdraw)
temp_s$`Question Type` <- "Support for Withdrawal"

temp_w <- iraq_withdraw_support
temp_w <- iraq_withdraw_support[ , (names(iraq_withdraw_support) %in% c("Start_polling", "Startdate", "NoSupport"))]
temp_w <- temp_w %>% rename(SurveyMean = NoSupport)
temp_w$`Question Type` <- "Lack of Support for War"

iraq_withdraw_support_long <- rbind(temp_s, temp_w)
rm(temp_s, temp_w)
iraq_withdraw_support_long$Startdate <- as.Date(iraq_withdraw_support_long$Startdate)

iraq_withdraw_support_long$Startdate_y <-as.Date(iraq_withdraw_support_long$Startdate,'%Y-%m-%d')
iraq_withdraw_support_long$Startdate_y <- format(as.Date(iraq_withdraw_support_long$Startdate_y, format="%Y/%m/%d"),"%Y")
iraq_withdraw_support_long$Startdate_y <- as.numeric(iraq_withdraw_support_long$Startdate_y)

iraq_withdraw_support_long$Startdate_m <- substr(iraq_withdraw_support_long$Startdate, start = 6, stop = 7)
iraq_withdraw_support_long$Startdate_m <- as.numeric(iraq_withdraw_support_long$Startdate_m)
iraq_withdraw_support_long$Startdate_m <- iraq_withdraw_support_long$Startdate_m*(1/12)-(1/12)

iraq_withdraw_support_long$Startdate_d <- substr(iraq_withdraw_support_long$Startdate, start = 9, stop = 10)
iraq_withdraw_support_long$Startdate_d <- as.numeric(iraq_withdraw_support_long$Startdate_d)
iraq_withdraw_support_long$Startdate_d <- iraq_withdraw_support_long$Startdate_d/365.25

iraq_withdraw_support_long$Startdate_long <- iraq_withdraw_support_long$Startdate_y+iraq_withdraw_support_long$Startdate_m+iraq_withdraw_support_long$Startdate_d

iraq_withdraw_support_long$SurveyMean_p <- iraq_withdraw_support_long$SurveyMean/100

### Figure S4
margin <- unit(0.5, "line")
grob1 <- ggplot(iraq_withdraw_support_long, aes(x=Startdate_long, y=SurveyMean_p)) +
  geom_line(aes(col = `Question Type`)) +
  geom_point(aes(col = `Question Type`, shape = `Question Type`)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits=c(0,1)) +
  scale_x_continuous(limits=c(2002,2011)) +
  labs(x = "Date of Poll", y = "Support for Withdrawal/Lack of Support for War", col='Question Type', shape="Question Type") +
  theme_minimal() +
  theme(text=element_text(family="Times New Roman"), plot.title = element_text(hjust = 0.5), legend.position="bottom")
grob2 <- textGrob("Figure S4: Support for Withdrawal vs. Lack of Support for War during the Iraq War", gp=gpar(fontsize=13, fontfamily="Times New Roman"))

plota4 <- grid.arrange(grob1, grob2,
                      heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin),
                      vp=viewport(width=0.95, height=0.95))
ggsave("Plots/Figure S4 - Support for Withdrawal vs. Lack of Support for War during the Iraq War.png", plota4)


rm(iraq_support, iraq_withdraw, iraq_withdraw_support, iraq_withdraw_support_long, short_support, short_withdraw)
rm(grob1, grob2, margin, plota4)

